<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('指标信息列表')" />
    <style>
        .layui-table-click {
            background-color:lightgrey;
        }
    </style>
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse" >
                <div class="form-group" style="float: right;">
                    <label class="checkbox-inline check-box">
                        <input type="checkbox" value="option2" onchange="showLableClass('后缀','hz')" id="hz">后缀</label>
                    <label class="checkbox-inline check-box">
                        <input type="checkbox" value="option3" onchange="showLableClass('指标属性','zbsx')" id="zbsx">指标属性</label>
                    <label class="checkbox-inline check-box">
                        <input type="checkbox" value="option3" onchange="showLableClass('场景','cj')" id="cj">场景</label>
                    <label class="checkbox-inline check-box">
                    <input type="text" class="form-control" style="width: 230px" placeholder="按ENTER键执行搜索" id="filter" />
                </div>
                <div class="themeItem" style="margin-top: 12px" name="cdr" th:each="entry:${labels}" th:attr="name=${entry.key}">
                    <span style="font-weight: bold;margin-right: 10px"  th:text="${entry.key} ">:</span>
                    <span class="label" style="cursor: pointer;margin-right: 7px" onclick="themeItemChange(this)" th:each="label:${entry.value}" th:text="${label.na}" th:attr="idLabel=${label.idLabel}"></span> <!--th:each="label:${labelList.current.value}"-->
                </div>
                <form id="formId">
                    <input name="idLabels" type="hidden" id="idLabels">
                    <input name="na" type="hidden" id="na">
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
                <p><span style="color: red">注意：IDX_开头的为普通指标，原则为一天处理一次。IDX_AT_开头的为实时指标，处理级别为分钟</span></p>

                <h4 class="form-header h4" style="cursor: pointer"><a href="#" onclick="selectDeptTree()">指标标签</a></h4>
                <div class="row" style="padding-left: 30px" id="labelDiv">
                </div>
            </div>

        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "base/md/index";
        var sdSysData = [[${@baseSd.getItem('hos_sys')}]];
        var valtpData = [[${@baseSd.getItem('index_valtp')}]];
        var G_IDINDEX=null;
        function showLableClass(na,id) {
            if($("#"+id).is(":checked")){
                $(".themeItem[name="+na+"]").show();
            } else {
                $(".themeItem[name="+na+"]").hide();
                $(".themeItem[name="+na+"] .label").removeClass("label-info");
            }
        };
        var tableRefresh=function() {
            let labels = $(".themeItem").find(".label");
            let idLabels = [];
            let idLabel;
            $.each(labels,function (index,item) {
                debugger;
                if($(item).hasClass("label-info")) {
                    idLabel = $(item).attr("idLabel");
                    idLabels.push(idLabel);
                }
            });
            $("#idLabels").val(idLabels);
            $("#na").val($("#filter").val());
            $("#bootstrap-table").bootstrapTable('refresh');
        }
        function themeItemChange(ele) {
            if($(ele).hasClass("label-info")) {
                $(ele).removeClass("label-info");
            } else {
                $(ele).addClass("label-info");
            }
            tableRefresh();
        }
        var refreshLabel = function () {
            $.ajax({
                url: "/base/label/findByIdIndex/"+G_IDINDEX,
                cache: false,
                type: 'GET',
                dataType: 'json',
                success: function (res) {
                    var html="";
                    if(res.code=="0") {
                        var data = res.data;
                        $.each(data,function (index,item) {
                            html+='<span class="label label-info" style="font-size: 15px;padding: 8px">'+item.na+'<a class="close-link" href="#" onclick="deleteIndexLabel('+item.idLabel+',\''+item.na+'\')"><i class="fa fa-times"></i></a></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                        });
                        $("#labelDiv").html(html);
                    }
                }
            });
        }
        function selectDeptTree() {
            if(G_IDINDEX==null) {
                $.modal.alertError("请先选择一项指标");
                return ;
            }
            var options = {
                title: '添加标签',
                width: "480",
                url: ctx+"base/md/index/labelTree?idIndex="+G_IDINDEX,
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }
        function doSubmit(index, layero){
            var iframeWin = layero.find('iframe')[0];
            iframeWin.contentWindow.submitHandler(index, layero);
            refreshLabel();
            $.modal.close(index);
        }
        function deleteIndexLabel(idLabel,naLabel) {
            $.modal.confirm("确认要删除标签["+naLabel+"]吗？", function() {
                $.ajax({
                    url: "/md/index/label/remove?idIndex="+G_IDINDEX+"&idLabel="+idLabel,
                    cache: false,
                    type: 'GET',
                    dataType: 'json',
                    success: function (res) {
                        refreshLabel();
                    }
                });
            });
        }
        function onClickRow(row, $element){
            $element.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            G_IDINDEX = row.idIndex;
            refreshLabel();
        }
        $(function() {
            $("#filter").bind('keyup',function (event) {
                if(event.keyCode=="13") {
                    tableRefresh();
                }
            });
            var options = {
                url: prefix + "/listByIdLabels",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "指标信息",
                onClickRow: onClickRow,
                columns: [{
                    checkbox: true
                },
                {
                    field: 'idIndex',
                    title: '指标ID'
                },
                {
                    field: 'na',
                    title: '指标名称'
                },
                {
                    field: 'sdSys',
                    title: '系统',
                    formatter: function(value, item, index) {
                        return $.table.selectSdNaByCd(sdSysData, item.sdSys);
                    }
                },
                {
                    field: 'instr',
                    title: '口径'
                },
                {
                    field: 'euTp',
                    title: '指标类型',
                    formatter: function(value, item, index) {

                        if(item.euTp==0) {
                            return "原子指标";
                        } else if(item.euTp==1) {
                            return "复合指标";
                        } else if(item.euTp==2) {
                            return "子指标";
                        }
                    }
                },
                {
                    field: 'fgAccu',
                    title: '累加标志',
                    formatter: function(value, item, index) {
                        if(item.fgAccu==1) {
                            return "可累加";
                        } else {
                            return "否"
                        }
                    }
                },
                {
                    field: 'des',
                    title: '备注'
                },
                {
                    field: 'sdValtp',
                    title: '值类型',
                    formatter: function(value, item, index) {
                        return $.table.selectSdNaByCd(valtpData, item.sdValtp);
                    }
                },
                {
                    field: 'unit',
                    title: '单位'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.idIndex + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.idIndex + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
            showLableClass('后缀','hz');
            showLableClass('指标属性','zbsx');
            showLableClass('场景','hz');
        });
    </script>
</body>
</html>